<template>
  <div>
    <a-row :gutter="[20, 0]">
      <a-col :span="6">
        <a-card
          :body-style="{ padding: '24px 32px' }"
          :bordered="false"
          class="menu"
        >
          <div
            style="
              display: flex;
              align-items: center;
              justify-content: space-between;
            "
          >
            <a-tree
              :tree-data="treeData"
              :replaceFields="replaceFields"
              :defaultExpandAll="true"
              @select="selectTreeNode"
            >
            </a-tree>
          </div>
        </a-card>
      </a-col>
      <a-col style="background: #fff" :span="18">
        <div class="conditionsSearch">条件搜索</div>
        <div class="search-wrapper">
          <!-- 头部搜索 -->

          <a-form-model
            class="topAntform"
            ref="searchFormRef"
            :model="searchFormData"
          >
            <a-form-model-item label="组织名称:" class="serach-input">
              <a-input
                placeholder="请输入"
                size="large"
                v-model="searchFormData.name"
              />
            </a-form-model-item>
          </a-form-model>
          <!-- 按钮组 -->
          <div class="btn-group">
            <a-button
              type="success"
              icon="search"
              @click="searchBtnClick"
              style="background-color: #0079fe; color: #fff"
            >
              查询
            </a-button>
            <!-- <a-button @click="resetBtnClick">重置</a-button> -->
          </div>
        </div>
        <a-card :body-style="{ padding: '10px 0px' }" :bordered="false">
          <div
            style="
              display: flex;
              align-items: center;
              justify-content: center;
              width: 100%;
              height: 30px;
              float: right;
              margin-bottom: 10px;
              justify-content: space-between;
            "
          >
            <span
              style="
                padding-left: 12px;
                color: rgba(0, 0, 0, 0.85);
                font-size: 14px;
              "
              >当前有<span style="padding: 0 3px">{{ tableData.length }}</span
              >条数据</span
            >
            <div style="text-align: right; cursor: pointer" class="handle">
              <!-- <a
                href="javascript:;"
                style="margin: 0 10px 0 0"
                @click="addResourceHandle"
                >新增</a
              > -->
              <a
                class="addStyle"
                style="margin-right: 8px; color: #fff"
                @click="addResourceHandle"
                v-permission:devManage="['add']"
              >
                <a-icon type="plus" />新增
              </a>
            </div>
          </div>

          <div style="margin-top: 40px">
            <a-table
              :columns="columns"
              :data-source="tableData"
              :pagination="pagination"
              size="small"
            >
              <template slot="operation" slot-scope="text, record, index">
                <div class="editable-row-operations">
                  <a
                    href="javascript:;"
                    style="margin: 0 10px"
                    @click="updateResourceHandle(text, record, index)"
                  >
                    编辑</a
                  >
                  <a
                    v-if="record.shield == 0"
                    href="javascript:;"
                    @click="enableResourceHandle(text, record, index)"
                    >禁用</a
                  >
                  <a
                    v-else
                    href="javascript:;"
                    @click="enableResourceHandle(text, record, index)"
                    >启用</a
                  >
                </div>
              </template>
            </a-table>
          </div>
        </a-card>
      </a-col>
    </a-row>
    <a-modal
      :title="title"
      width="40%"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="visible = false"
    >
      <a-form-model
        ref="ruleForm"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 14 }"
        :model="menuObj"
        :rules="rules"
      >
        <a-form-model-item
          v-if="selectVisible"
          ref="parentName"
          label="上级组织"
          prop="parentName"
        >
          {{ parentObj.name }}
        </a-form-model-item>

         <a-form-model-item
          v-else
          ref="parentName"
          label="上级组织"
          prop="parentName"
        >
          {{ menuObj.pname }}
        </a-form-model-item>
        <a-form-model-item label="名称" prop="name">
          <a-input v-model="menuObj.name" />
        </a-form-model-item>
        <a-form-model-item label="部门类型" prop="type">
          <a-radio-group v-model="menuObj.type">
            <a-radio value="ENTERPRISE"> 企业</a-radio>
            <a-radio value="DEP"> 部门</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="组织编码" prop="code">
          <a-input
            placeholder="字母和数字组合"
            v-model="menuObj.code"
          ></a-input>
        </a-form-model-item>
        <a-form-model-item label="组织描述">
          <a-input v-model="menuObj.orgDesc"></a-input>
        </a-form-model-item>
        <!-- <a-form-model-item label="部门负责人">
          <a-input v-model="menuObj.principalUserId"></a-input>
        </a-form-model-item>
        <a-form-model-item label="上级分管领导">
          <a-input v-model="menuObj.partLeaderUserId"></a-input>
        </a-form-model-item> -->
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import {
  queryOrgTreeA,
  queryListPageZjjgA,
  zzjgSaveA,
  zzjgBjHandle,
  zzjgEnableA,
} from "../../services/system";

export default {
  name: "MenuManage",
  data() {
    return {
      title: "新增",
      selectVisible: true,
      searchFormData: {
        name: "",
      },
      appList: [],
      channelList: [],
      query: {
        appInfoCode: null,
        channelType: null,
      },
      searchValue: "",
      treeData: [],
      replaceFields: {
        title: "name",
        key: "id",
        children: "child",
      },
      visible: false,
      confirmLoading: false,
      parentObj: {},
      menuObj: {
        name: null,
        type: null,
        parentId: null,
        orgDesc: null,
        code: null,
        tenantCode: sessionStorage.getItem("tenantCode"),
      },
      rules: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        code: [{ required: true, message: "不能为空", trigger: "blur" }],
        type: [{ required: true, message: "不能为空", trigger: "blur" }],
        isShow: [{ required: true, message: "请选择", trigger: "change" }],
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
      columns: [
        {
          title: "组织名称",
          dataIndex: "name",
        },
        {
          title: "组织编号",
          dataIndex: "code",
        },
        {
          title: "人数",
          dataIndex: "userNumber",
        },

        {
          title: "创建时间",
          dataIndex: "createTime",
        },
        {
          title: "操作",
          dataIndex: "Action",
          scopedSlots: { customRender: "operation" },
        },
      ],
      tableData: [],
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    getRoleList() {
      if (sessionStorage.getItem("roleType") == "TENTANT_ADMIN") {
        this.roleType = "NORMAL";
      } else if (sessionStorage.getItem("roleType") == "PLATFORM_ADMIN") {
        this.roleType = "TENTANT_ADMIN";
      }
      const parma = {
        parentId: this.parentObj.id,

        tenantCode: sessionStorage.getItem("tenantCode"),
      };
      queryListPageZjjgA(parma).then((res) => {
        this.tableData = res.data.data.rows;
      });
      const parmas = {
        tenantCode: sessionStorage.getItem("tenantCode"),
      };
      queryOrgTreeA(parmas).then((res) => {
        this.treeData = res.data.data;
        console.log("1===", this.treeData);
      });
    },
    selectTreeNode(selectedKeys, e) {
      this.parentObj = e.node.dataRef;
      console.log("1====", this.parentObj);
      this.menuObj.parentId = this.parentObj.id;
      this.menuObj.tenantCode = sessionStorage.getItem("tenantCode");
      const parma = {
        parentId: this.parentObj.id,

        tenantCode: sessionStorage.getItem("tenantCode"),
      };
      queryListPageZjjgA(parma).then((res) => {
        this.tableData = res.data.data.rows;
      });
    },

    //新增按钮

    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.confirmLoading = true;
          let handle;

          if (this.menuObj.id) {
            this.menuObj.parentId = this.menuObj.pid
            handle = zzjgBjHandle(this.menuObj.id, this.menuObj);
          } else {
            console.log(this.menuObj);
            handle = zzjgSaveA(this.menuObj);
          }
          handle
            .then((res) => {
              this.confirmLoading = false;
              if (res.data.status == 0) {
                this.visible = false;
                this.getRoleList();
              }
            })
            .catch((error) => {
              console.log(error);
              this.confirmLoading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //新增
    addResourceHandle() {
      this.title = "新增";
      // if (this.parentObj.name) {
      this.dataType = 1;
      this.menuObj = {
        name: null,
        parentId: this.parentObj.id ? this.parentObj.id : "",
        type: null,
        orgDesc: null,
        code: null,
        tenantCode: sessionStorage.getItem("tenantCode"),
      };
      this.visible = true;
      // } else {
      //   this.$message.error("请选择一个菜单");
      // }
    },
    //编辑
    updateResourceHandle(text, item) {
      this.title = "编辑";
      this.selectVisible = false;
      console.log(item);
      this.dataType = 1;
      this.menuObj = item;
      // this.getMenuById(item.id);
      this.visible = true;
    },

    //禁用、启用
    enableResourceHandle(text, item) {
      let vm = this;

      if (item.shield == 0) {
        //要去禁用
        vm.title = "您确定要禁用该数据？";
        item.shield = 1;
      } else {
        //要去启用
        vm.title = "您确定要启用该数据？";
        item.shield = 0;
      }
      this.$confirm({
        title: vm.title,
        onOk() {
          return new Promise((resolve, reject) => {
            zzjgEnableA(item.id, item.shield).then((res) => {
              if (res.data.status == 0) {
                vm.getRoleList();
                resolve();
              } else {
                reject(res.data.msg);
              }
            });
          });
        },
        onCancel() {},
      });
    },

    //搜索
    searchBtnClick() {
      const parma = {
        orgId: this.menuObj.orgId,
        name: this.searchFormData.name,
        tenantCode: sessionStorage.getItem("tenantCode"),
      };
      queryListPageZjjgA(parma).then((res) => {
        this.tableData = res.data.data.rows;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.conditionsSearch {
  border: 1px solid #eee;
  color: rgba(0, 0, 0, 0.85);
  font-size: 14px;
  padding: 6px 12px;
  border-radius: 4px 4px 0 0;
  margin-top: 15px;
}
.search-wrapper {
  border-radius: 0 0 4px 4px;
  display: flex;
  background-color: #fff;
  align-items: center;
  border: 1px solid #eee;
  border-top: none;
  padding-left: 12px;
  padding-bottom: 8px;
}
.topAntform {
  width: 313px;
  height: 48px;
}
.serach-input {
  display: flex;
  padding-top: 5px;
  input {
    margin-left: 10px;
    height: 30px;
  }
}
.btn-group {
  padding: 0;
  button {
    background-color: #fff !important;
    color: #666 !important;
  }
}
/deep/ .ant-card {
  padding-top: 0;
}
.addStyle {
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgb(0, 121, 254);
  width: 78px;
  height: 30px;
}
.menu {
  .handle {
    i {
      margin: 0 10px;
      font-size: 16px;
      cursor: pointer;
    }
  }
}
</style>
